<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增库存')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-tInventory-add" th:object="${tRecor}">
			<input name="deptId" type="hidden" id="treeId" th:value="${deptId}" />
			<input type="hidden"  th:value="*{variety}" id="varietyId"/>
			<div class="form-group">
				<label class="col-sm-3 control-label">条形码：</label>
				<div class="col-sm-8">
					<input id="barcode" name="barcode" class="form-control" type="text" required>
				</div>
			</div>
			<!--<div class="form-group">
				<label class="col-sm-3 control-label">饰品名称：</label>
				<div class="col-sm-8">
					<input id="name" name="name" class="form-control" type="text" >
				</div>
			</div>-->

			<div class="form-group">
					<label class="col-sm-3 control-label">门店：</label>
					<div class="col-sm-8">
						<div class="input-group">
							<input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择归属部门" class="form-control" required readonly="true" th:value="${deptName}">
							<span class="input-group-addon"><i class="fa fa-search"></i></span>
						</div>
					</div>
				</div>

			<div class="form-group">	
				<label class="col-sm-3 control-label">品类：</label>
				<div class="col-sm-8">
					<select name="category" class="form-control" required>
						<option value="">--请选择品类--</option>
						<option th:each="level : ${category}" th:value="${level.typeId}" th:text="${level.typeName}" th:field="*{category}"></option>
					</select>
				</div>
			</div>

			<div class="form-group">	
				<label class="col-sm-3 control-label">品种：</label>
				<div class="col-sm-8">
					<select id="variety" name="variety" class="form-control" required>
						<option value="">--请选择品种--</option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">重量(g)：</label>
				<div class="col-sm-8">
					<input id="weight" name="weight" class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">标签价格：</label>
				<div class="col-sm-8">
					<input id="labelprice" name="labelprice" class="form-control" type="text" required>
				</div>
			</div>


		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "system/tInventory"
		$("#form-tInventory-add").validate({
			rules:{
                barcode:{
                    remote: {
                        url: prefix + "/checkBarcodeUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "barcode" : function() {
                                return $.common.trim($("#barcode").val());
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
			},
            messages: {
                "barcode": {
                    remote: "条形码已经存在"
                }
            },
			focusCleanup: true
		});

		function queryDataByCode() {
			var barcode = $("#barcode").val();
            $.ajax({
                type: "get",
                url: ctx + 'system/tInventory/queryDataByCode/' + barcode,
                dataType: "json",
                cache: false, //关闭AJAX缓存
                data: null,
                success: function (data) {
                   if(data.data.length>0){
                       $.modal.msgError("条形码已经存在，请重新输入！");
				   }
                }
            });
        }
		
		function submitHandler() {
	        if ($.validate.form()) {
                var varietyName = $("#variety option:selected").text()
	            $.operate.save(prefix + "/add", $('#form-tInventory-add').serialize()+"&name="+varietyName);
	        }
	    }

		/*用户管理-新增-选择部门树*/
		function selectDeptTree() {
            var deptId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();
			var options = {
				title: '选择部门',
				width: "380",
				url: ctx + "system/dept/selectDeptTree/" + deptId,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}

		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
				var body = layer.getChildFrame('body', index);
				$("#treeId").val(body.find('#treeId').val());
				$("#treeName").val(body.find('#treeName').val());
				layer.close(index);
			}
		}

		$("select[name='category']").change(function () {
			var id = $("select[name='category']").val();
			getLevel2(id);
		});

        getLevel2();
		function getLevel2(id) {
            var id = id == undefined?$("select[name='category']").val():id;
			$.ajax({
				type: "get",
				url: ctx + 'system/types/getLevel2/' + id,
				dataType: "json",
				cache: false, //关闭AJAX缓存
				data: null,
				success: function (data) {
					var levelList = data.data;
                    var varietyId = $("#varietyId").val();
					$("select[name='variety']").empty();
					$("select[name='variety']").append("<option value=\">--请选择品种--</option>");
                    $.each(levelList, function (i, d) {
                        if(d.typeId == varietyId){
                            $("select[name='variety']").append("<option value='" + d.typeId + "' selected='selected'>" + d.typeName + "</option>");
                        }else{
                            $("select[name='variety']").append("<option value='" + d.typeId + "'>" + d.typeName + "</option>");
                        }
                    });
				}
			});
		}
	</script>
</body>
</html>
